import {createRouter,createWebHistory,RouteRecordRaw} from 'vue-router'
import {useMainStore} from '@/store'
import Home from '../views/Home.vue';
import Login from '../views/login.vue';
import layout from '../layout/index.vue'
const routes:Array<RouteRecordRaw>=[{
    path:"",
	redirect:'/index',
    component:layout,
	children:[
		{
			path:'/index',
			component:Home,
			name:'index',
			meta:{title:'首页'}
		}
	]
},{
    path:"/login",
    name:"login",
    component:Login
}]


const router=createRouter({
    history:createWebHistory(process.env.BASE_URL),
    routes:routes,
	// 记录页面滚动条位置	scrollBehavior(to,from,savedPosition){
		if (savedPosition) {
		      return savedPosition
		    } else {
		      return { top: 0 }
		 }
	}
})


router.beforeEach((to,from,next)=>{
	const store = useMainStore()
	if(to.path=='/login'){
		next()
	}else{
		const token = store.$state.userInfo; 
		if (token === null || token === '') {
			// next('/login');
			next();
		} else {
			next();
		}
	}
})

export default  router